﻿using crip.Common;
using crip.Database;
using crip.Models;
using crip.Services.IRepositorys;
using crip.Services.Repositorys;
using Microsoft.EntityFrameworkCore;

namespace crip.Services.Repositorys
{
    public class AreaRepository : BaseRepository<Area>, IAreaRepository
    {
        #region 依赖注入
        public CripDbContext _cripDbContext;

        public AreaRepository(CripDbContext cripDbContext) : base(cripDbContext)
        {
            _cripDbContext = cripDbContext;
        }

        #endregion

        #region 添加area: AddAreaAsync
        /// <summary>
        /// 添加该area
        /// </summary>
        /// <param name="area">area</param>
        /// <returns></returns>
        public async Task AddAreaAsync(Area area)
        {
           await _cripDbContext.Areas.AddAsync(area);
        }
        #endregion

        #region 分页获得多个Area: GetAllAreaAsync
        /// <summary>
        /// 分页获得多个Area
        /// </summary>
        /// <param name="keyword">模糊查找键值</param>
        /// <param name="orderby">安装什么排序，默认name，可选number，place</param>
        /// <param name="desc">是否倒叙，默认否</param>
        /// <param name="pageNumber">页数</param>
        /// <param name="pageSize">页面内容数量</param>
        /// <returns></returns>
        public async Task<PageList<Area>> GetAllAreaAsync(string? keyword, string? orderby,bool? desc, int pageNumber, int pageSize)
        {
            //获取集合
            IQueryable<Area> result = _cripDbContext.Areas
                .Include(a => a.CripUsers);
          
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                keyword = keyword.Trim();
                var   result1 = result.Where(t => t.Place.Contains(keyword));
                var   result2 = result.Where(t => t.Name.Contains(keyword));
                //Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>)通过使用默认的相等比较器，生成两个序列的并集
                result = result2.Union(result1);
            }

            result = orderby switch
                {
                    "number" =>(desc == true)?result.OrderByDescending(a => a.Members_Number):result.OrderBy(a => a.Members_Number),
                    "place" =>(desc == true) ? result.OrderByDescending(a => a.Place): result.OrderBy(a => a.Place),
                    _ => (desc == true) ? result.OrderByDescending(a => a.Name):result.OrderBy(a => a.Name)
                };

            return await PageList<Area>.CreateAsync(pageNumber,pageSize,result);
        }
        #endregion

        #region 删除用户的AreaId 通过 areaId
        public void DeleteUserAreaByAreaId(Guid areaId)
        {
            _cripDbContext.Users.Where(u => u.AreaId == areaId).ToList().ForEach(u => u.AreaId = null);
        }
        #endregion

        #region 通过AreaId获取Area: GetAreaByAreaIdAsync
        /// <summary>
        /// 通过区域Id获得区域（包含成员数据）
        /// </summary>
        /// <param name="guid">areaId</param>
        /// <returns>查找到的Area</returns>
        public async Task<Area> GetAreaByAreaIdAsync(Guid? areaId)
              {
                return await _cripDbContext.Areas.Where(a => a.Id == areaId)
                                                 .Include(a => a.CripUsers)
                                                 .FirstOrDefaultAsync();
               }
               #endregion
              
    }
}
